// components/product/product-sku/index.js
import componentBehaviors from '../../../behaviors/component';
import { COMPONENT_OPTIONS } from '../../../constants/common';
import { tipToast } from '../../../common/env';
import { getMaxNumber } from '../../../common/utils';

Component({
    behaviors: [componentBehaviors],
    options: {
        ...COMPONENT_OPTIONS,
    },
    properties: {
        show: {
            type: Boolean,
            value: false,
        },
        zIndex: {
            type: Number,
            value: 120,
        },
        productInfo: {
            type: Object,
            value: {},
        },
        // 数量可选
        canLimitCount: {
            type: Boolean,
            value: false,
        },
        limitCount: {
            type: Number,
            value: 0,
        },
    },
    observers: {
        'productInfo': function (val) {
            if (val && val.productAttributeDefs) {
                if (val.productAttributeDefs.length) {
                    this.setData({
                        propertyList: val.productAttributeDefs.map(item => {
                            return {
                                ...item,
                                selectProperty: '',
                            };
                        }),
                    });
                } else {
                    this.setData({
                        selectSkuTip: '无规格',
                        selectSku: val.skus[0],
                    });
                }
            }
        },
        'show, limitCount': function (show, count) {
            show && count && this.setData({ count });
        },
    },
    data: {
        propertyList: [],
        selectSkuTip: '未选择',
        selectSku: null,
        count: 0,
        maxCount: getMaxNumber(),
    },
    methods: {
        // 关闭
        onClose: function () {
            this.setData({
                show: false,
                propertyList: [],
                selectSkuTip: '未选择',
                selectSku: null,
                count: 0,
            });
            this.triggerEvent('close');
        },
        // 选择属性
        choosePropertyItem: function (event) {
            const { key, property } = event.currentTarget.dataset;
            // 选择属性，然后匹配其他可选的属性
            const list = this.data.propertyList;
            const targetList = list.map(item => {
                if (item.key == key) {
                    return {
                        ...item,
                        selectProperty: property,
                    };
                }
                return item;
            });
            console.log('ChooseProductSku => 选择后的列表为', targetList);
            this.setData({
                propertyList: targetList,
            }, () => {
                this.dealSelectSku();
            });
        },
        // 生成选中的规格
        dealSelectSku: function () {
            const { propertyList, productInfo, count } = this.data;
            const arr = [];
            for (let i = 0; i < propertyList.length; i++) {
                const item = propertyList[i];
                if (item.selectProperty) {
                    arr.push(item.selectProperty);
                }
            }
            console.log('ChooseProductSku => 选中的数据', arr.join('/'));
            const selectSkuTip = arr.join('/');
            const skusList = productInfo.skus;
            const list = skusList.filter(item => item.skuName == selectSkuTip);
            const selectSku = list.length > 0 ? list[0] : null;
            if (selectSku && selectSku.maximum != -1) {
                const maxCount = selectSku.maximum - selectSku.freezeQuantity;
                if (count && count > maxCount) {
                    this.setData({ count: maxCount });
                }
            }
            this.setData({
                selectSkuTip,
                selectSku,
            });
        },
        // 修改数量
        changeCount: function (event) {
            console.log('ChooseProductSku => 修改数量', event.detail);
            this.setData({
                count: event.detail,
            });
        },
        // 确认选择
        ensureClick: function () {
            const { productInfo, selectSku, count } = this.data;
            if (count > 0) {
                this.triggerEvent('ensure', {
                    ...productInfo,
                    selectSku,
                    count,
                });
                this.onClose();
            } else {
                tipToast('请选择数量');
            }
        },
    }
})
